<template>
  <div>
    <el-dialog
      :title="title"
      center
      :visible.sync="open"
      @close="close()"
      append-to-body
    >
      <el-form
        ref="editForm"
        :model="editForm"
        :rules="rules"
        label-width="110px"
      >
        <el-form-item
          :label="$t('batch.systemConfig.form.actuatorName')"
          prop="executorName"
        >
          <el-input
            v-model="editForm.executorName"
            :placeholder="$t('batch.missionManage.rules.pleaseEnter')"
          />
        </el-form-item>
        <el-form-item
          :label="$t('batch.systemConfig.form.actuatorCode')"
          prop="executorCode"
        >
          <el-input
            v-model="editForm.executorCode"
            :placeholder="$t('batch.missionManage.rules.pleaseEnter')"
            :disabled="disabled"
          />
        </el-form-item>
        <el-form-item
          :label="$t('batch.systemConfig.form.actuatorType')"
          prop="executorType"
        >
          <el-select
            v-model="editForm.executorType"
            :placeholder="$t('batch.missionManage.rules.pleaseSelect')"
            style="width: 100%"
          >
            <el-option
              v-for="item in dictMap.executor_type"
              :key="item.value"
              :label="item.text"
              :value="Number(item.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          :label="$t('batch.systemConfig.form.channelShared')"
          prop="channelShared"
        >
          <el-select
            v-model="editForm.channelShared"
            :placeholder="$t('batch.missionManage.rules.pleaseSelect')"
            style="width: 100%"
          >
            <el-option
              v-for="item in dictMap.sys_yes_no"
              :key="item.value"
              :label="item.text"
              :value="Number(item.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('common.remark')" prop="remark">
          <el-input
            v-model="editForm.remark"
            :placeholder="$t('batch.missionManage.rules.pleaseEnter')"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">{{
          $t("common.confirm")
        }}</el-button>
        <el-button @click="close">{{ $t("common.cancel") }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { dialogMixin } from "@/libs/dialogMixin";
import {
  get,
  save,
  update,
  checkExecutorCodeUnique,
  checkExecutorNameUnique,
} from "@/api/batch_app/systemConfig/actuator";
export default {
  name: "EditActuator",
  mixins: [dialogMixin],
  components: {},
  data() {
    // 表单校验
    let checkExecutorCodeUniqueRule = (rule, value, callback) => {
      if (!value) {
        callback(new Error(this.$t("batch.systemConfig.rules.cannotEmpty")));
      } else {
        checkExecutorCodeUnique({
          sysConfId: this.sysConfId,
          executorCode: value,
          id: this.editForm.id,
        }).then((response) => {
          var result = response.result;
          if (!result) {
            callback(
              new Error(this.$t("batch.systemConfig.rules.alreadyExists"))
            );
          } else {
            callback();
          }
        });
      }
    };

    let checkExecutorNameUniqueRule = (rule, value, callback) => {
      if (!value) {
        callback(new Error(this.$t("batch.systemConfig.rules.nameNotEmpty")));
      } else {
        checkExecutorNameUnique({
          sysConfId: this.sysConfId,
          executorName: value,
          id: this.editForm.id,
        }).then((response) => {
          var result = response.result;
          if (!result) {
            callback(
              new Error(this.$t("batch.systemConfig.rules.nameAlreadyExists"))
            );
          } else {
            callback();
          }
        });
      }
    };
    return {
      open: false,
      disabled: false,
      sysConfId: "",
      title: "",
      rules: {
        executorName: [
          {
            required: true,
            trigger: "blur",
            validator: checkExecutorNameUniqueRule,
          },
        ],
        executorCode: [
          {
            required: true,
            trigger: "blur",
            validator: checkExecutorCodeUniqueRule,
          },
        ],
        executorType: [
          {
            required: true,
            message: this.$t("batch.systemConfig.rules.mustChoose"),
            trigger: "blur",
          },
        ],
        channelShared: [
          {
            required: true,
            message: this.$t("batch.systemConfig.rules.mustChoose"),
            trigger: "blur",
          },
        ],
      },
    };
  },
  mounted() {
    this.loadDicts(["sys_yes_no", "executor_type"]);
  },
  methods: {
    show(id, sysConfId, disabled = false) {
      this.disabled = disabled;
      this.sysConfId = sysConfId;
      this.$set(this, "editForm", {});
      if (id) {
        get({
          id: id,
        }).then((res) => {
          this.$set(this, "editForm", res.result);
          this.editForm.sysConfId = sysConfId;
        });
        this.title = this.$t("common.edit");
      } else {
        this.title = this.$t("common.add");
        this.editForm.sysConfId = sysConfId;
      }
      this.open = true;
    },
    submitForm: function () {
      this.$refs["editForm"].validate((valid) => {
        if (valid) {
          if (this.editForm.id) {
            delete this.editForm.createTime;
            delete this.editForm.updateTime;
            update(this.editForm).then((res) => {
              this.msgSuccess(this.$t("common.modifySuccess"));
              this.open = false;
              this.$emit("editComplete", true);
            });
          } else {
            delete this.editForm.createTime;
            delete this.editForm.updateTime;
            save(this.editForm).then((res) => {
              this.msgSuccess(this.$t("common.addSuccess"));
              this.open = false;
              this.$emit("editComplete", true);
            });
          }
        }
      });
    },
  },
};
</script>

<style scoped>
</style>
